<?php namespace Phpcmf\Controllers\Member;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Dianzan extends \Phpcmf\Home\Module
{
	public function __construct() {
        parent::__construct();
    }

	public function index() {
		//判断是post
		if (IS_POST) {
			//get 参数
			$id = (int)\Phpcmf\Service::L('input')->post('id'); //帖子id
			$cid = (int)\Phpcmf\Service::L('input')->post('cid'); // 圈子id
			
			$shuju_data = \Phpcmf\Service::M()->db->table('1_wenda')->where('id', $id)->where('status', 1)->get()->getRowArray();
			//标题
			if($shuju_data['title']){
				$zantitle = $shuju_data['title'];
			} else {
				$zantitle = dr_strcut(dr_clearhtml($shuju_data['neirong']), 20);
			}
			// 判断 
			$dianzan_data = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->where('uid', $this->uid)->countAllResults();
			if($dianzan_data){
				// 已经点赞了 那么就删除点赞
				\Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->where('uid', $this->uid)->delete();
				$dianzan_tongji = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->countAllResults();
				$data = [
					'code' => 0,
					'data' => $dianzan_tongji,
					'uid'=> $this->uid,
				];

				$this->_json('0', '已取消点赞',$data);
			} else {
				// 插入数据
				$insert_data = [
					'tieid' => $id,
					'uid' => $this->uid,
					'shenhe' => 1,
					'tianjiashijian' => time(),
				];
				$insert_id = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->insert($insert_data);
				if($insert_id){
					$dianzan_tongji = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->countAllResults();
					$data = [
						'code' => 1,
						'data' => $dianzan_tongji,
						'uid'=> $this->uid,
		
					];
					$dataduixin = [
						'catid'=> 4, //类型
						'title' => '点赞', //标题
						'zhiding' => '', //置顶
						'content'=> $zantitle, //通知内容
						'uid'=> $this->uid, //发布人的uid
						'faxinren'=> $this->uid, //发布人的uid
						'shouxinren'=> (int)$shuju_data['uid'],//收信人
						'tupian'=> '', //图片
						'shipin'=> '', //视频
						'yuyin'=> '', //语音
						'fujian'=> '', //附件
						'yidu'=> 0, //已读
						'quanziid'=> $cid, //圈子ID
						'quantieid'=> $id, //圈帖ID
						'url' => '',
					];
					if($this->uid != (int)$shuju_data['uid']){
						// 通知
						\Phpcmf\Service::M('auth', 'duixin')->addtongzhi($dataduixin);
					}
					
					
	
					$this->_json('1', '点赞成功',$data);
				}
			}
			//$this->_json('1', '点赞成功',$dianzan_data);
		} else {
			$data = [
				'code' => 0,
				'data' => '无效提交',
				'uid'=> 0,

			];

			$this->_json('0', '无效提交');
		}
		
		
		

	}
	public function  dianzan_panduan() {
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id'); //帖子id
		
		$dianzan_data = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->where('uid', $this->uid)->countAllResults();
		$dianzan_tongji = \Phpcmf\Service::M()->db->table('1_wenda_dianzan')->where('tieid', $id)->countAllResults();
		if($dianzan_data){
			$data = [
				'code' => 1,
				'data' => $dianzan_tongji,
				'uid'=> $this->uid,
				'admin' => $this->member,

			];

			
			$this->_json('1', '已点赞',$data);
		} else {
			$data = [
				'code' => 0,
				'data' => $dianzan_tongji,
				'uid'=> $this->uid,
				'admin' => $this->member,
			];
			$this->_json('0', '未点赞',$data);
		}
		
		
	}

}
